package com.yq.cp.service.basics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.basics.Area;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.Provinces;
import com.yq.cp.mapper.basics.*;
import com.yq.cp.service.basics.AreaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 值班时间管理 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Resource
    AreaMapper areaMapper;
    @Resource
    ProvincesMapper provincesMapper;
    @Resource
    CitiesMapper citiesMapper;
    @Resource
    AreasMapper areasMapper;

    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;
    @Override
    public void add(Area area) {
        if (!Objects.isNull(area.getId())) {
            if (area.getParentId() == 0) {
                OrganizationalStructure structure = organizationalStructureMapper.selectOne(new QueryWrapper<OrganizationalStructure>()
                        .eq("associationId", area.getId()));
                structure.setName(area.getName());
                organizationalStructureMapper.updateById(structure);
            }else {

            }
            areaMapper.updateById(area);
        }else {
            if (area.getParentId() == 0) {
                OrganizationalStructure structure = new OrganizationalStructure();
                structure.setName(area.getName());
                structure.setType(2);
                organizationalStructureMapper.insert(structure);
            }else {
                OrganizationalStructure structure = organizationalStructureMapper.selectOne(new QueryWrapper<OrganizationalStructure>()
                        .eq("associationId", area.getParentId()));
                Provinces byId1 =provincesMapper.selectOne(new QueryWrapper<Provinces>()
                        .eq("provinceid",area.getShengId()));

                OrganizationalStructure structureSheng = new OrganizationalStructure();
                structure.setName(area.getName());
                structure.setType(3);
                structure.setParentId(structure.getId());

                organizationalStructureMapper.insert(structureSheng);

            }
            areaMapper.insert(area);
        }
    }
}
